package com.killer.business.listener;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RocketMQMessageListener(topic = "ad_update",consumerGroup = "g1")
@Slf4j
public class AdUpdateListener implements RocketMQListener<String> {

    @Value("${ad.updateUrl}")
    private String adUpdateUrl;

    @Override
    public void onMessage(String position) {
        log.info("收到广告更新通知消息，位置是:{}", position);
        //拼接url
        String requestUrl=adUpdateUrl+"?position="+position;
        OkHttpClient okHttpClient=new OkHttpClient();
        Request request = new Request.Builder().url(requestUrl).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.error("更新广告缓存失败", e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                log.info("更新广告缓存成功，返回消息:{}", response.message());
            }
        });
    }

}
